package com.example.demo.test;

import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.Browser;
import com.ruiyun.jvppeteer.core.browser.BrowserFetcher;
import com.ruiyun.jvppeteer.core.page.Page;
import com.ruiyun.jvppeteer.options.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

public class jvppeteer {
    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        long l = System.currentTimeMillis();
        /*String path= new String("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe".getBytes(), "UTF-8");*/
        //自动下载，第一次下载后不会再下载
        BrowserFetcher.downloadIfNotExist(null);
        ArrayList<String> arrayList = new ArrayList<>();
        //生成pdf必须在无厘头模式下才能生效
        LaunchOptions options = new LaunchOptionsBuilder().withHeadless(true).withArgs(arrayList).withViewport(new Viewport(1024, 600, 1, false, false, false)).build();
        arrayList.add("--no-sandbox");
        arrayList.add("--disable-setuid-sandbox");
        Browser browser = Puppeteer.launch(options);
        Page page = browser.newPage();
        page.goTo("http://www.qq.com/");
        ScreenshotOptions screenshotOptions = new ScreenshotOptions();
        /*screenshotOptions.setFullPage(Boolean.TRUE);*///全屏截图
        //设置截图范围
        Clip clip = new Clip(0, 0, 1024, 600);

        screenshotOptions.setClip(clip);
        //设置存放的路径
        screenshotOptions.setPath("src/main/resources/static/image/test.png");
/*        Thread.sleep(1000);
        for (int i = 0; i < 10; i++) {
            page.evaluate("window.scrollBy(0,100)");
            Thread.sleep(2 * 1000);
        }*/
        page.screenshot(screenshotOptions);
        System.out.println(System.currentTimeMillis() - l + "毫秒");
        page.close();
        browser.close();
    }

}
